﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Threading;
using Ds.U6;
using DaInterFace;
using DaInterFace.UMode;


namespace DaSub.UMode
{
    public class UMode : IUMode
    {
        IDataCenter _daCenter;
        Dictionary<String, IUModeHole> _holeList = new Dictionary<String, IUModeHole>();
        Dictionary<String, IUModeGate> _gateList = new Dictionary<String, IUModeGate>();
        /// <summary>
        /// 放料口
        /// </summary>
        public Dictionary<String, IUModeHole> HoleList
        {
            get { return _holeList; }
        }
        /// <summary>
        /// 放料口
        /// </summary>
        public Dictionary<String, IUModeGate> GateList
        {
            get { return _gateList; }
        }

        public event MessageHandler MessageEvent;
        public event AddUModeHoleHandler AddHoleEvent;
        public event AddUModeGateHandler AddGateEvent;
        public event ClearUModeItemHandler ClearEvent;

        Thread _mainThread;

        public UMode(IDataCenter daCenter)
        {
            _daCenter = daCenter;
            _daCenter.AddUMode(this);
            Load();
        }

        public void Load()
        {
            if (_mainThread != null)
            {
                _mainThread.Abort();
                _mainThread = null;
            }
            foreach (IUModeHole item in HoleList.Values)
            {
                item.Dispose();
            }
            foreach (IUModeGate item in GateList.Values)
            {
                item.Dispose();
            }

            _mainThread = new Thread(Start);
            _mainThread.IsBackground = true;
            _mainThread.Name = "主启动线程";
            _mainThread.Start();
        }

        public void Clear()
        {
            _gateList.Clear();
            _holeList.Clear();
            if (ClearEvent != null)
            {
                ClearEvent.Invoke();
            }
        }

        public void Message(MessageType type, String note)
        {
            Message(type, note, 0);
        }

        public void Message(MessageType type, String note, Double span)
        {
            if (MessageEvent != null)
            {
                MessageEvent.Invoke(type, note, span);
            }
        }

        private void Start()
        {
            Clear();
            try
            {
                Recordset rs = _daCenter.DaData.ExeBase("Select FNumber,FName,FText1 IP,IsNull(FComboBox1,0) MatchStyle,FText2 FItemInfo,FCheckBox1 ForbidRepeat FROM KEW_ChargingEntry Where FCheckBox=0");
                
                if (rs.Enable)
                {
                    foreach (DataRow row in rs.Data.Tables[0].Rows)
                    {
                        IUModeHole item = new UModeHole(_daCenter, row["IP"].ToString(), (MatchStyle)int.Parse(row["MatchStyle"].ToString()), row["FItemInfo"].ToString(), row["FName"].ToString(), Common.CBool(row["ForbidRepeat"].ToString()));
                        _holeList.Add(row["FNumber"].ToString(), item);
                        if (AddHoleEvent != null)
                        {
                            AddHoleEvent.Invoke(item);
                        }
                    }
                }
                else
                {
                    Message(MessageType.Error, rs.Message);
                }
            }
            catch (Exception ex)
            {
                Message(MessageType.Error, ex.Message);
            }


            try
            {
                Recordset rs = _daCenter.DaData.ExeBase("Select FNumber,FName,FText IP,FComboBox GateType FROM KEW_AccessControlEntry Where FCheckBox=0");

                if (rs.Enable)
                {
                    foreach (DataRow row in rs.Data.Tables[0].Rows)
                    {
                        IUModeGate item = new UModeGate(_daCenter, row["IP"].ToString(), (EnumGateType)int.Parse(row["GateType"].ToString()), row["FName"].ToString(), false);
                        _gateList.Add(row["FNumber"].ToString(), item);
                        if (AddGateEvent != null)
                        {
                            AddGateEvent.Invoke(item);
                        }
                    }
                }
                else
                {
                    Message(MessageType.Error, rs.Message);
                }
            }
            catch (Exception ex)
            {
                Message(MessageType.Error, ex.Message);
            }
        }
    }

    internal enum MatchStyle : int
    {
        MatchName = 0,
        MatchNumber = 1,
        MatchPrice = 2,
        MatchItem = 3
    }
}
